﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using DCTLib.Common;
using DCTAccess.Business;
using DCTAccess.Business.Administrator;

namespace DCTLib.Common
{
    public class AppSessionInfo
    {
        #region Private
        private static HttpContext Context
        {
            get
            {
                return HttpContext.Current;
            }
        }

        private static bool ExistValue(string key)
        {
            return (Context.Session[key] != null);
        }

        private static object GetValue(string key)
        {
            if (Context.Session[key] != null)
            {
                return Context.Session[key];
            }
            else
            {
                return null;
            }
        }

        private static void SetValue(string key, object value)
        {
            Context.Session[key] = value;
        }
        #endregion

        public static bool IsUserRoleExisted(string key)
        {
            return ExistValue(key);
        }

        public static Administrator CurrentAdministrator
        {
            get
            {
                if (!ExistValue(AppSessionKeys.ADMIN_INFO))
                {
                    return null;
                }
                else
                {
                    return (Administrator)GetValue(AppSessionKeys.ADMIN_INFO);
                }
            }
            set
            {
                SetValue(AppSessionKeys.ADMIN_INFO, value);
            }
        }

        public static UserRole CurrentRole
        {
            get
            {
                if (ExistValue(AppSessionKeys.USER_INFO))
                {
                    return UserRole.Members;
                }               
                else if (ExistValue(AppSessionKeys.ADMIN_INFO))
                {
                    return UserRole.Administrator;
                }
                else
                {
                    return UserRole.Anonymous;
                }
            }
        }
    }
}
